/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import data.Requirement;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author stana
 */
@Stateless
public class RequirementFacade extends AbstractFacade<Requirement> {
    @PersistenceContext(unitName = "SmsWeatherPU")
    private EntityManager em;

    protected EntityManager getEntityManager()
    {
        return em;
    }

    public List<Requirement> findByDate(Date date)
    {
        return em.createNamedQuery("Requirement.findByDate").setParameter("date", date).getResultList();
    }

    public List<Requirement> findByPhone(long phoneNumber)
    {
        return em.createNamedQuery("Requirement.findByPhoneNumber").setParameter("phoneNumber", phoneNumber).getResultList();
    }

    public List<Requirement> findRangePhone(int[]range,long phoneNumber)
    {
        Query q=em.createNamedQuery("Requirement.findByPhoneNumber").setParameter("phoneNumber", phoneNumber);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();

    }

    public int countPhoneNumber(long phoneNumber)
    {
        return em.createNamedQuery("Requirement.findByPhoneNumber").setParameter("phoneNumber", phoneNumber).getResultList().size();
    }

    public RequirementFacade()
    {
        super(Requirement.class);
    }
    public void flush()
    {
        em.flush();
    }

}
